package com.loongtech.bi.action.pay;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.pay.ActiveUser5LevelFirstAddcashManager;
import com.loongtech.bi.manager.pay.HabitPayManager;
import com.loongtech.bi.manager.pay.HabitPayManager.FirstPayCount;
import com.loongtech.bi.manager.pay.HabitPayManager.FirstPayLevel;
import com.loongtech.bi.manager.pay.HabitPayManager.FirstPayPlayTime;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * 付费分析 —付费习惯Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class HabitPayAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private List<FirstPayCount> firstPayCountData;  // 首付金额数据
	private ChartUIData firstPayCountChart;  // 首付金额图表
	
	private List<FirstPayLevel> firstPayLevelData;  // 首付等级数据
	private ChartUIData firstPayLevelChart;  // 首付等级图表
	
	private List<FirstPayPlayTime> firstPayPlayTimeData;  // 首付金额数据
	private ChartUIData firstPayPlayTimeChart;  // 首付金额图表
	
	private String levleName = "1-5等级";  // 首付等级选中的等级数
	private Map <String,String> firstpayLevelMap;  // 首付等级可选的等级数
	private String firstPayLevelOption = "userCount";  // 首付游戏时长图表切换项（userCount=人数,payCount=金额），默认为人数
	
	private String firstPayPlayTimeOption = "days";  // 首付游戏时长图表切换项（days=天数,time=累计时长），默认为天数
	
	@Autowired
	private HabitPayManager habitPayManager;
	@Autowired
	private ActiveUser5LevelFirstAddcashManager activeUser5LevelFirstAddcashManager;
	
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;
	
	@PostConstruct
	public void init() {
		//设置等级段
		setFirstPaylevelMap();
		
		search();
	}

	public void search() {
		//设置等级段
		setFirstPaylevelMap();
		
		sysLogManager.addLog(session.getUserName(), "付费分析 —付费习惯");
		
		firstPayCountChart = null;
		firstPayLevelChart = null;
		firstPayPlayTimeChart = null;
		
		firstPayCountData = habitPayManager.getFirstPayCountData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		
		if (firstPayCountData != null && firstPayCountData.size() > 0) {
			firstPayCountChart = new ChartUIData();
			firstPayCountChart.setJsonType(ChartType.BAR2.type);
			firstPayCountChart.setJsonStr(new ChartTool(firstPayCountData, FirstPayCount.class, "").loadData(ChartType.BAR.type));
		}
		
		firstPayLevelData = habitPayManager.getFirstPayLevelData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
        Collections.sort(firstPayLevelData, habitPayManager.new FirstPayLevel().comparatorLevel);
        if (firstPayLevelData != null && firstPayLevelData.size() > 0) {
			firstPayLevelChart = new ChartUIData();
			firstPayLevelChart.setJsonType(ChartType.BAR2.type);
			firstPayLevelChart.setJsonStr(new ChartTool(firstPayLevelData, FirstPayLevel.class, firstPayLevelOption).loadData(ChartType.BAR.type));
		}
		
		firstPayPlayTimeData = habitPayManager.getFirstPayPlayTimeData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		if (firstPayPlayTimeData != null && firstPayPlayTimeData.size() > 0) {
			firstPayPlayTimeChart = new ChartUIData();
			firstPayPlayTimeChart.setJsonType(ChartType.BAR2.type);
			firstPayPlayTimeChart.setJsonStr(new ChartTool(firstPayPlayTimeData, FirstPayPlayTime.class, "").loadData(ChartType.BAR.type));
		}
	}
	
	//设置等级map
	private void setFirstPaylevelMap() {
		firstpayLevelMap = new LinkedHashMap<>();
		
		List<Object[]> list = activeUser5LevelFirstAddcashManager.getFirstAddcashLevelNameByAllKind(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		
		int index = 0;
		
		for(Object o :list) {
			String level_name = String.valueOf(o);
			firstpayLevelMap.put(level_name, level_name);
			
			if(index == 0) {
				levleName = level_name;
			}
			
			index ++;
		}
	}
	
	// 首付等级分布切换等级分组
	/*public void handleFirstPayLevelChange() {
		firstPayLevelChart = null;
		
		firstPayLevelData = habitPayManager.getFirstPayLevelData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer,levleName);
		if (firstPayLevelData != null && firstPayLevelData.size() > 0) {
			firstPayLevelChart = new ChartUIData();
			firstPayLevelChart.setJsonType(ChartType.BAR2.type);
			firstPayLevelChart.setJsonStr(new ChartTool(firstPayLevelData, FirstPayLevel.class, firstPayLevelOption).loadData(ChartType.BAR.type));
		}
	}*/
	
	// 首付等级切换
	public void selectedFirstPayLevelOption() {
		firstPayLevelChart.setJsonStr(new ChartTool(firstPayLevelData, FirstPayLevel.class, firstPayLevelOption).loadData(ChartType.BAR.type));
	}
	
	// 首付游戏时长切换图表项调用的方法
	public void selectedFirstPayPlayTimeOption() {
		firstPayPlayTimeChart = null;
		
		firstPayPlayTimeData = habitPayManager.getFirstPayPlayTimeData(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		if (firstPayPlayTimeData != null && firstPayPlayTimeData.size() > 0) {
			firstPayPlayTimeChart = new ChartUIData();
			firstPayPlayTimeChart.setJsonType(ChartType.BAR2.type);
			firstPayPlayTimeChart.setJsonStr(new ChartTool(firstPayPlayTimeData, FirstPayPlayTime.class, "").loadData(ChartType.BAR.type));
		}
	}
	
	@Override
	protected String csvContent() {
		return null;
	}

	@Override
	protected String csvFileName() {
		return null;
	}

	public List<FirstPayCount> getFirstPayCountData() {
		return firstPayCountData;
	}

	public void setFirstPayCountData(List<FirstPayCount> firstPayCountData) {
		this.firstPayCountData = firstPayCountData;
	}

	public ChartUIData getFirstPayCountChart() {
		return firstPayCountChart;
	}

	public void setFirstPayCountChart(ChartUIData firstPayCountChart) {
		this.firstPayCountChart = firstPayCountChart;
	}

	public List<FirstPayPlayTime> getFirstPayPlayTimeData() {
		return firstPayPlayTimeData;
	}

	public void setFirstPayPlayTimeData(List<FirstPayPlayTime> firstPayPlayTimeData) {
		this.firstPayPlayTimeData = firstPayPlayTimeData;
	}

	public ChartUIData getFirstPayPlayTimeChart() {
		return firstPayPlayTimeChart;
	}

	public void setFirstPayPlayTimeChart(ChartUIData firstPayPlayTimeChart) {
		this.firstPayPlayTimeChart = firstPayPlayTimeChart;
	}

	public String getFirstPayPlayTimeOption() {
		return firstPayPlayTimeOption;
	}

	public void setFirstPayPlayTimeOption(String firstPayPlayTimeOption) {
		this.firstPayPlayTimeOption = firstPayPlayTimeOption;
	}

	public List<FirstPayLevel> getFirstPayLevelData() {
		return firstPayLevelData;
	}

	public void setFirstPayLevelData(List<FirstPayLevel> firstPayLevelData) {
		this.firstPayLevelData = firstPayLevelData;
	}

	public ChartUIData getFirstPayLevelChart() {
		return firstPayLevelChart;
	}

	public void setFirstPayLevelChart(ChartUIData firstPayLevelChart) {
		this.firstPayLevelChart = firstPayLevelChart;
	}
	
	public String getLevleName() {
		return levleName;
	}

	public void setLevleName(String levleName) {
		this.levleName = levleName;
	}

	public Map<String, String> getFirstpayLevelMap() {
		return firstpayLevelMap;
	}

	public void setFirstpayLevelMap(Map<String, String> firstpayLevelMap) {
		this.firstpayLevelMap = firstpayLevelMap;
	}

	public String getFirstPayLevelOption() {
		return firstPayLevelOption;
	}

	public void setFirstPayLevelOption(String firstPayLevelOption) {
		this.firstPayLevelOption = firstPayLevelOption;
	}
}
